<?php
/**
 * Created by PhpStorm.
 * Author: MeiJay
 * Date: 2018/11/6
 * Time: 18:59
 */

namespace App\Http\Controllers\Web\V1;


use App\Exceptions\WebExceptions\WebException;
use App\Service\Activity\SponsorsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SponsorsController extends BaseController
{
    /**
     * 赞助商列表
     * @param SponsorsService $prizeService
     * @author: 梅杰 20181106
     *
     */
    public function sponsors(SponsorsService $sponsorsService)
    {
        $re = $sponsorsService->getAllList();
        success($re);
    }

    /**
     * 添加奖品
     * @throws WebException
     * @author: 梅杰 20101106
     */
    public function addSponsors(Request $request)
    {
        $input = $request->only(['name','desc','xcx_app_id','xcx_page','official_accounts','logo']);
        //数据验证
        $messages = [
            'name.required'     => '请填写赞助商名称!',
            'desc.required'    => '请填写赞助商描述!',
        ];
        $validator = Validator::make($input,[
            'name' => 'required' ,
            'desc' => 'required' ,
        ],$messages);

        if ($validator->fails()) {
            throw new WebException($validator->errors());
        }
        if ((new SponsorsService())->add($input)) {
            success();
        }
        throw new WebException('添加失败');
    }

    /**
     * @param $id
     * @throws WebException
     * @author: 梅杰 20181106
     */
    public function updateSponsors($id)
    {
        $input = app('request')->only(['name','desc','xcx_app_id','xcx_page','official_accounts','logo']);

        //数据验证
        $messages = [
            'name.required'     => '请填写赞助商名称!',
            'desc.required'    => '请填写赞助商描述!',
        ];
        $validator = Validator::make($input,[
            'name' => 'required' ,
            'desc' => 'required' ,
        ],$messages);

        if ($validator->fails()) {
            throw new WebException($validator->errors());
        }
        $service = (new SponsorsService());
        if ($service->saveRow($id,$input) !== false) {
            success();
        }
        throw new WebException('保存失败');
    }

    /**
     * 获取奖品详情
     * @param $id
     * @author: 梅杰 20181106
     */
    public function getSponsors($id)
    {
        $service = (new SponsorsService());
        $data = $service->getRow($id);
        success($data);
    }

    /**
     * 删除奖品
     * @param $id
     * @throws WebException
     * @author: 梅杰 20181106
     */
    public function deleteSponsors($id)
    {
        if ((new SponsorsService())->del($id) !== false) {
            success();
        }
        throw new WebException('删除失败');
    }
}